package com.hnist.empdemo.utils;

import com.hnist.empdemo.mapper.Impl.UserMapperImpl;
import com.hnist.empdemo.pojo.User;

/**
 * 密码管理器，用于处理密码修改相关功能
 */
public class PasswordManager {
    
    private String currentUser;
    
    public PasswordManager() {
    }
    
    public PasswordManager(String currentUser) {
        this.currentUser = currentUser;
    }
    
    /**
     * 验证原密码是否正确
     * @param oldPassword 用户输入的原密码
     * @return 如果原密码正确返回true，否则返回false
     */
    public boolean validateOldPassword(String oldPassword) {
        // 从数据库中获取当前用户信息并验证密码
        if (currentUser != null && !currentUser.isEmpty()) {
            User user = UserMapperImpl.getInstance().findByUsername(currentUser);
            if (user != null) {
                return user.getPassword().equals(oldPassword);
            }
        }
        return false;
    }
    
    /**
     * 验证新密码和确认密码是否一致
     * @param newPassword 新密码
     * @param confirmPassword 确认密码
     * @return 如果一致返回true，否则返回false
     */
    public boolean validateNewPasswordMatch(String newPassword, String confirmPassword) {
        return newPassword.equals(confirmPassword);
    }
    
    /**
     * 验证密码强度
     * @param password 要验证的密码
     * @return 如果密码符合要求返回true，否则返回false
     */
    public boolean validatePasswordStrength(String password) {
        // 简单的密码强度验证：长度不少于6位
        return password != null && password.length() >= 6;
    }
    
    /**
     * 更新用户密码
     * @param newPassword 新密码
     * @return 如果更新成功返回true，否则返回false
     */
    public boolean updatePassword(String newPassword) {
        try {
            if (currentUser != null && !currentUser.isEmpty()) {
                User user = UserMapperImpl.getInstance().findByUsername(currentUser);
                if (user != null) {
                    user.setPassword(newPassword);
                    return UserMapperImpl.getInstance().updateUser(user);
                }
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    
    /**
     * 修改密码的完整流程
     * @param oldPassword 原密码
     * @param newPassword 新密码
     * @param confirmPassword 确认密码
     * @return PasswordChangeResult 包含修改结果和消息
     */
    public PasswordChangeResult changePassword(String oldPassword, String newPassword, String confirmPassword) {
        // 验证输入是否为空
        if (oldPassword == null || oldPassword.isEmpty() || 
            newPassword == null || newPassword.isEmpty() || 
            confirmPassword == null || confirmPassword.isEmpty()) {
            return new PasswordChangeResult(false, "所有字段都必须填写");
        }
        
        // 验证原密码是否正确
        if (!validateOldPassword(oldPassword)) {
            return new PasswordChangeResult(false, "原密码不正确");
        }
        
        // 验证新密码和确认密码是否一致
        if (!validateNewPasswordMatch(newPassword, confirmPassword)) {
            return new PasswordChangeResult(false, "新密码和确认密码不一致");
        }
        
        // 验证密码强度
        if (!validatePasswordStrength(newPassword)) {
            return new PasswordChangeResult(false, "密码长度不能少于6位");
        }
        
        // 更新密码
        if (updatePassword(newPassword)) {
            return new PasswordChangeResult(true, "密码修改成功");
        } else {
            return new PasswordChangeResult(false, "密码修改失败");
        }
    }
    
    /**
     * 密码修改结果类
     */
    public static class PasswordChangeResult {
        private boolean success;
        private String message;
        
        public PasswordChangeResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public String getMessage() {
            return message;
        }
    }
}